package educationaladministrationsystem.CSM.Controller;

import educationaladministrationsystem.Class.Count;
import educationaladministrationsystem.Result.Result;
import educationaladministrationsystem.CSM.Mapper.Service.LoginService;
import educationaladministrationsystem.Tool.JWTUnits;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//登录注册
@RestController
@Slf4j
@RequestMapping("LoginAndSign")
public class LoginController {
    @Autowired
    LoginService loginService;

    @GetMapping("/CountLog")
    public Result isHaveCount(Count count) {
        boolean haveAboutCount = loginService.isHaveAboutCount(count);
        String jwt = null;          // 记录token
        if (haveAboutCount) {
            Map<String, Object> map = new HashMap<>();
            map.put("countNumber", count.getCountNumber());
            map.put("password", count.getPassword());
            map.put("identification", count.getIdentification());
            map.put("id", count.getId());
            jwt = JWTUnits.generateJwt(map);
        }
        return Result.success(jwt);
    }

    @PostMapping("/CountSign")
    public Result checkCount(@RequestBody Count count) {
        return Result.success(loginService.insertAboutCount(count));
    }

    @PostMapping("/CountPasswordUpdate")
    public Result PasswordUpdate(@RequestParam String newPassword, @RequestParam String countNumber) {
        loginService.PasswordUpdate(newPassword, countNumber);
        return Result.success();
    }

    @GetMapping("/getUID")
    public Result getUID(@RequestParam String countNumber) {
        return Result.success(loginService.getUID(countNumber));
    }
}
